package com.novel.authorization.vip;

import com.novel.base.config.properties.CustomVipProperties;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.schema.Column;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * VipHandler
 *
 * @author 樊温军
 * @date 2025/3/25 下午2:52
 */
@Slf4j
@Component
public class CustomVipHandler {
    @Resource
    private CustomVipProperties customVipProperties;

    public Expression getVip() {
        // 从当前系统上下文中取出当前请求的Vip等级，通过解析器注入到SQL中。
        Integer vip = VipContextHolder.getVip();
        log.info("当前Vip等级为{}", vip);
        if (vip == null) {
            return new NullValue();
        }
        return new LongValue(vip);
    }

    public String getVipColumn() {
        return customVipProperties.getColumn();
    }

    public boolean ignoreTable(String tableName) {
        return customVipProperties.getTables().stream().noneMatch((e) -> e.equalsIgnoreCase(tableName));
    }

    public boolean ignoreInsert(List<Column> columns, String vipColumn) {
        return columns.stream().map(Column::getColumnName).noneMatch((i) -> i.equalsIgnoreCase(vipColumn));
    }
}
